package com.cdplife.ci.core.common.ftp;

import javax.servlet.http.HttpServletRequest;

import com.cdplife.ci.core.common.constant.Const;
import com.cdplife.ci.core.util.ImageUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.util.Iterator;
import java.util.List;

public class UploadResult {

	public boolean success = true;

	public String message;

	public Integer id;

	public String basePath;

	public String thumbPath;

	public String filePath;

	public String fileSize;

	public String fileName;


	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getThumbPath() {
		return thumbPath;
	}

	public String getThumbPathUrl() {
		if (thumbPath.contains(Const.TMP_DIR)) {
			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
			return request.getContextPath() + thumbPath;
		} else {
			//获取OSS图片地址
			return FtpOss.getImgUrl(thumbPath);
		}
	}

	public void setThumbPath(String thumbPath) {
		this.thumbPath = thumbPath;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getFilePathUrl() {
		if (filePath.contains(Const.TMP_DIR)) {
			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
			return request.getContextPath() + filePath;
		} else {
			//获取OSS图片地址
			return FtpOss.getImgUrl(filePath);
		}
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getFileSize() {
		return fileSize;
	}

	public void setFileSize(String fileSize) {
		this.fileSize = fileSize;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getBasePath() {
		return basePath;
	}

	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}

	/**
	 * @brief 获取上传结果
	 * @param request
	 * @param accessoryTable 所属附件表名 做一级目录
	 * @param refId 所属附件表主键ID 做二级目录
	 * @param accessoryType 所属附件表主键附件类型 做三级级目录
	 * @return
	 * @throws Exception
	 */
	public UploadResult(HttpServletRequest request,String accessoryTable,Integer refId,Integer accessoryType) throws Exception {
		if (request instanceof MultipartHttpServletRequest) {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			Iterator<String> files = multipartRequest.getFileNames();
			while (files.hasNext()) {
				String fileName = files.next();
				List<MultipartFile> uploadFiles = multipartRequest.getFiles(fileName);
				if (uploadFiles.size() > 0) {
					MultipartFile uploadFile = uploadFiles.get(0);
					if (uploadFile.getSize() != 0) {
						fileSize = String.valueOf( uploadFile.getSize());
						String newFileName = uploadFile.getOriginalFilename();
						basePath = request.getServletContext().getRealPath("/");
						String toDir = Const.TMP_DIR + accessoryTable + "/" + refId  + "/" + accessoryType ;
						String ossPath = Const.OSS_DIR + accessoryTable + "/" + refId  + "/" + accessoryType ;
						File dir = new File(basePath + toDir);
						if (!dir.exists()) {
							dir.mkdirs();
						}
						File newFile = new File(basePath + toDir, newFileName);
						uploadFile.transferTo(newFile);
						//通过文件名判断是否图片 图片需要创建缩放图
						if(ImageUtil.isImage(ImageUtil.getFileExtention(newFileName))){
							ImageUtil.generateThumbnail2Directory(basePath+toDir , basePath+toDir+ "/"+newFileName);
							thumbPath = ossPath + "/" + ImageUtil.appendSuffix(newFileName, "-thumbnail");
						}
						filePath = ossPath + "/" + newFileName;
						this.fileName = newFileName;
					}
				}
			}
		}
	}
}
